<template>
  <el-dialog title="分配角色" :visible.sync="isShow" @close="closeEvent">
    <!-- checkbox
              label:选中该项后的值
              v-model='数组/非数组'
              数组：选中后在数组内添加该项的值，如果没有设置label,它的值是null
              非数组：选中后它的值为true,取消选中为false
     -->
    <el-checkbox
      v-for="item in list"
      :key="item.id"
      v-model="roleIds"
      :label="item.id"
    >{{ item.name }}</el-checkbox>

    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees'
export default {
  data() {
    return {
      isShow: false,
      list: [],
      roleIds: [],
      id: ''
    }
  },
  created() {
    // this.getData()
  },
  methods: {
    async getData() {
      if (this.list.length === 0) {
        const res = await sysRole({
          page: 1,
          pagesize: 99999
        })
        this.list = res.data.rows
        console.log('角色列表', res)
      }
    },
    // 获取当前员工详情
    async getRole(id) {
      this.id = id
      const res = await sysUser(id)
      this.roleIds = res.data.roleIds
      console.log(res)
    },
    // 提交点击
    async submit() {
      await sysUserAssignRoles({
        id: this.id,
        roleIds: this.roleIds
      })
      this.isShow = false
      this.$message.success('分配角色成功')
    },
    closeEvent() {
      this.roleIds = []
    }
  }
}
</script>
<style></style>
